<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="330" destroy-on-close @closed="$emit('closed')">
		<el-form :model="classFormData" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
			<el-form-item v-if="parentInfo && editType == 'two'" label="上级" prop="name">
				{{parentInfo.name}}
			</el-form-item>
			<el-form-item label="类别名称" prop="name">
				<el-input v-model="classFormData.name" clearable placeholder="请输入类别名称"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑'
				},
				visible: false,
				isSaveing: false,
				classFormData: {
					name: "",
				},
				rules: {
					name: [{
						required: true,
						message: '请输入大类名称'
					}]
				},
				dic: [],
				dicProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						// let children_t = 0
						// this.classFormData.itemList.forEach(item=>{
						// 	console.log(item)
						// 	children_t += item.passScore
						// })
						let data = {
							name: this.classFormData.name,
							passScore: this.classFormData.passScore || 0,
							demo: "",
						}
						if (this.classFormData && this.classFormData.id) {
							data.id = this.classFormData.id
						}
						if (this.parentInfo && this.parentInfo.id) {
							data.parent = {
								id: this.parentInfo.id
							}
						}
						const res = await this.$API.api.ptemp.save.post(data)
						if (res.code == 200) {
							this.visible = false;
							this.$emit('success', this.classFormData, this.mode)
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(parent, data, type) {

				if (this.mode == 'add' && type == 'one') {
					this.parentInfo = parent
				}
				if (this.mode == 'add' && type == 'two') {
					this.parentInfo = parent
				}
				if (this.mode == 'edit' && type == 'one') {
					this.parentInfo = parent
					this.classFormData = data
				}
				if (this.mode == 'edit' && type == 'two') {
					this.parentInfo = parent
					this.classFormData = data
				}
				// this.classFormData.id = pdata.id
				// this.classData = cdata
				// if(this.mode == 'add'){
				// 	this.classInfo = data
				// }else{
				// 	this.classFormData = data
				// }
				//this.classFormData = data
				console.log(66663, data)
				// this.classFormData.name = data.name
				// this.classFormData.code = data.code
				// this.classFormData.parentId = data.parentId

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.classFormData, data)
				//this.parentInfo = data
				this.editType = type
				// if(data && type == 'two'){
				// 	this.parentInfo = data.data
				// }else{
				// 	if(this.editType == 'two'){
				// 		Object.assign(this.classFormData, data)
				// 	}

				// 	console.log(this.classFormData)
				// }
			}
		}
	}
</script>

<style>
</style>
